package com.zhonghui.dolo.manager.controller;

import com.zhonghui.dolo.manager.model.vo.*;
import com.zhonghui.dolo.manager.service.GoodsCategoryService;
import com.zhonghui.dolo.manager.util.Constans;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class GoodsCategoryController {

    @Autowired
    private GoodsCategoryService goodsCategoryService;

    @ResponseBody
    @RequestMapping("/goods/category/list")
    public ResultVo<List<SimpleGoodsCategoryVo>> list(@RequestParam("parentId") Integer parentId) {
        ResultVo<List<SimpleGoodsCategoryVo>> resultVo = new ResultVo<>();
        List<SimpleGoodsCategoryVo> goodsCategoryVos = goodsCategoryService.findCategoryListByParentId(parentId);
        resultVo.setRes(Constans.RES_SUCCESS);
        resultVo.setObj(goodsCategoryVos);
        return resultVo;
    }

    @ResponseBody
    @RequestMapping("/mgr/goods/category/list")
    public PageResultVo<List<GoodsCategoryDetailVo>> listV2(@RequestBody GoodsCategoryReqVo goodsCategoryReqVo) {
        int pageSize = goodsCategoryReqVo.getLimit() != null ? goodsCategoryReqVo.getLimit() : Constans.DEFAULT_PAGE_SIZE;
        int pageNum = goodsCategoryReqVo.getOffset() != null ? (goodsCategoryReqVo.getOffset() / pageSize) + 1 : 1;
        PageResultVo<List<GoodsCategoryDetailVo>> goodsCategoryVos = goodsCategoryService.findCategoryList(pageNum, pageSize, goodsCategoryReqVo);
        return goodsCategoryVos;
    }

    @ResponseBody
    @RequestMapping("/mgr/goods/category/add")
    public ResultVo<String> add(@RequestBody GoodsCategoryEditVo goodsCategoryEditVo) {
        ResultVo<String> resultVo = new ResultVo<>();
        int result = goodsCategoryService.add(goodsCategoryEditVo);
        if (result > 0) {
            resultVo.setObj("添加成功");
            resultVo.setRes(Constans.RES_SUCCESS);
        } else {
            resultVo.setObj("添加失败");
            resultVo.setRes(Constans.RES_FAIL);
        }
        return resultVo;
    }
}
